package com.controller;

import com.pojo.*;
import com.serivce.UserManagerSerivce;
import com.util.Controller;
import com.util.RequestMapping;
import com.util.Util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Controller
public class UserManagerController {
    @RequestMapping("/home")
    public String home(HttpServletRequest request, HttpServletResponse response){
        return "login";
    }

    //登录页面
    @RequestMapping("/userLogin")
    public  String userLogin(HttpServletRequest request, HttpServletResponse response){
        String account = request.getParameter("account");
        String password = request.getParameter("password");

        UserManagerSerivce um = new UserManagerSerivce();
        Users users = um.userLogin(account, password);
        if (users!=null){
            request.getSession().setAttribute("currentUser",users);
            return (users.getRoleid()==1) ? "teacher":"student";
        }else {
            request.setAttribute("msg","账号或密码错误");
            return "login";
        }
    }

    //获取用户信息
    @RequestMapping("/rePage")
    public  Users rePage(HttpServletRequest request, HttpServletResponse response){
        Users users = (Users) request.getSession().getAttribute("currentUser");
        return users;
    }
    //退出页面
    @RequestMapping("/userExit")
    public String userExit(HttpServletRequest request, HttpServletResponse response){
        request.getSession().invalidate();
        return "login";
    }
    //查询所有用户
    @RequestMapping("/queryAllUser")
    public List<Users> queryAllUser(HttpServletRequest request, HttpServletResponse response){
        UserManagerSerivce um = new UserManagerSerivce();
        List<Users> users = um.queryAllUser();
        return users;
    }
    //添加学生用户
    @RequestMapping("/addUser")
    public String addUser(HttpServletRequest request, HttpServletResponse response){
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        int classid = Integer.parseInt(request.getParameter("classid"));

        int roleid = 2;
        Users users = new Users();
        users.setRoleid(roleid);
        users.setAccount(account);
        users.setPassword(password);
        users.setName(name);
        users.setClassid(classid);

        UserManagerSerivce um = new UserManagerSerivce();
        int result = um.addUser(users);
        if (result!=0){
            return "text:OK";
        }
        return null;
    }
    //查询所有课程
    @RequestMapping("/queryAllCourse")
    public List<Course> queryAllCourse(HttpServletRequest request, HttpServletResponse response){
        UserManagerSerivce um = new UserManagerSerivce();
        List<Course> courses = um.queryAllCourse();
        System.out.println(courses);
        request.getSession().setAttribute("courselist",courses);
        return courses;
    }
    //查询所有班级
    @RequestMapping("/queryAllClass")
    public List<Pjclass> queryAllClass(HttpServletRequest request, HttpServletResponse response){
        UserManagerSerivce um = new UserManagerSerivce();
        List<Pjclass> classes = um.queryAllClass();
        return classes;
    }
    //创建试卷
    @RequestMapping("/createExam")
    public String createExam(HttpServletRequest request, HttpServletResponse response){
        String pname = request.getParameter("pname");
        String cno = request.getParameter("cno");
        String userid = request.getParameter("userid");
        String classid = request.getParameter("classid");
        String singlenumber = request.getParameter("singlenumber");
        String decidenumber = request.getParameter("decidenumber");
        String examdate = request.getParameter("examdate");
        String examend = request.getParameter("examend");
        String examtime = request.getParameter("examtime");

        UserManagerSerivce um = new UserManagerSerivce();
        List<Course> courses = um.queryAllCourse();
        int cno1 = 0;
        for (Course cours : courses) {
            if (cours.getCname().equals(cno)){
                cno1 = cours.getCno();
            }
        }
        List<Users> users = um.queryAllUser();
        int userid1 = 0;
        for (Users user : users) {
            if (user.getAccount().equals(userid)){
                userid1 = user.getUserid();
            }
        }
        List<Pjclass> pjclasses = um.queryAllClass();
        int classid1 = 0;
        for (Pjclass pjclass : pjclasses) {
            if (pjclass.getClassname().equals(classid)){
                classid1 = pjclass.getClassid();
            }
        }
        Date examdate1 = Util.dateStr4Date(examdate);
        Date examend1 = Util.dateStr4Date(examend);

        Exam exam = new Exam();
        exam.setPname(pname);
        exam.setCno(cno1);
        exam.setUserid(userid1);
        exam.setClassid(classid1);
        exam.setSinglenumber(Integer.parseInt(singlenumber));
        exam.setSinglecore(50);
        exam.setDecidenumber(Integer.parseInt(decidenumber));
        exam.setDecidecore(30);
        exam.setExamdate(examdate1);
        exam.setExamend(examend1);
        exam.setExamtime(Integer.parseInt(examtime));

        int result = um.addExam(exam);
        if (result!=0){
//            System.out.println(result);
            return "text:OK";
        }
        return null;
    }
    //id查询考试
    @RequestMapping("/queryByEId")
    public Exam queryByEid(HttpServletRequest request, HttpServletResponse response){
        String eid = request.getParameter("eid");
        System.out.println(eid);
        UserManagerSerivce um = new UserManagerSerivce();
        Exam result = um.queryById(eid);
        return result;
    }
    //查询试卷
    @RequestMapping("/queryAllExam")
    public List<Paper> queryAllExam(HttpServletRequest request, HttpServletResponse response){
        UserManagerSerivce um = new UserManagerSerivce();
        List<Paper> paperList = um.queryAllExam();

        return paperList;
    }
    //ID查询试卷
    @RequestMapping("/queryByPid")
    public List<Subject> queryByPid(HttpServletRequest request, HttpServletResponse response){
        String pid = request.getParameter("pid");
        UserManagerSerivce um = new UserManagerSerivce();

        List<Subject> result = um.queryByPid(pid);
        Collections.shuffle(result);
        return result;
    }
    //删除试卷
    @RequestMapping("/deletePaper")
    public String deletePaper(HttpServletRequest request, HttpServletResponse response) {
        String pid = request.getParameter("pid");
        System.out.println(pid);
        UserManagerSerivce um = new UserManagerSerivce();
        int result = um.deletePaper(pid);
        if (result!=0){
            return "text:OK";
        }
        return null;
    }
    //添加成绩
    @RequestMapping("/addScore")
    public String addScore(HttpServletRequest request, HttpServletResponse response){
        String userid = request.getParameter("userid");
        String classid = request.getParameter("classid");
        String eid = request.getParameter("eid");
        String pname = request.getParameter("pname");
        String zscore = request.getParameter("zscore");
        String score = request.getParameter("score");

        Date tjtime = new Date();
//        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String tjtime = dateFormat.format(date);

        StudentExam studentExam = new StudentExam();
        studentExam.setUserid(Integer.parseInt(userid));
        studentExam.setClassid(Integer.parseInt(classid));
        studentExam.setEid(Integer.parseInt(eid));
        studentExam.setPname(pname);
        studentExam.setZscore(Integer.parseInt(zscore));
        studentExam.setScore(Integer.parseInt(score)*10);
        studentExam.setTjtime(tjtime);

        UserManagerSerivce um = new UserManagerSerivce();
        int result = um.addScore(studentExam);
        if (result!=0){
            return "text:OK";
        }
        return null;
    }
    //查询成绩
    @RequestMapping("/queryByUserid")
    public List<StudentExam> queryByUserid(HttpServletRequest request, HttpServletResponse response){
        String userid = request.getParameter("userid");
        UserManagerSerivce um = new UserManagerSerivce();

        List<StudentExam> result = um.queryByUserid(userid);

//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String strdate = "";
//        for (StudentExam res : result) {
//            Date date1 = new Date(String.valueOf(res.getTjtime()));
//            strdate = sdf.format(date1);
//            try {
//                res.setTjtime(sdf.parse(strdate));
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }
//        }
//        System.out.println(result);
        return result;
    }
}
